package com.linsen.erp.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsen.core.common.util.ConvertUtil;
import com.linsen.erp.admin.entity.EmployeeContactEntity;
import com.linsen.erp.admin.mapper.EmployeeContactMapper;
import com.linsen.erp.admin.provider.dto.EmployeeContactDTO;
import com.linsen.erp.admin.provider.vo.EmployeeContactVO;
import com.linsen.erp.admin.service.IEmployeeContactService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 员工-紧急联系人 服务实现类
 * </p>
 *
 * @author zhaoyong
 * @since 2024-10-08
 */
@Service
@RequiredArgsConstructor
public class EmployeeContactServiceImpl extends ServiceImpl<EmployeeContactMapper, EmployeeContactEntity> implements IEmployeeContactService {

    @Override
    public List<EmployeeContactVO> listByEmployeeId(String employeeId) {
        return ConvertUtil.convert(this.list(
                new LambdaQueryWrapper<EmployeeContactEntity>()
                        .eq(EmployeeContactEntity::getEmployeeId,employeeId)
        ), EmployeeContactVO.class);
    }

    @Transactional
    @Override
    public void set(String employeeId, List<EmployeeContactDTO> employeeContactDTOList) {
        //清除历史数据
        this.remove(
                new LambdaQueryWrapper<EmployeeContactEntity>()
                        .eq(EmployeeContactEntity::getEmployeeId,employeeId)
        );

        if (ObjectUtil.isNotEmpty(employeeContactDTOList)){
            List<EmployeeContactEntity> employeeContactEntityList = employeeContactDTOList.stream().map(employeeContactDTO -> {
                EmployeeContactEntity employeeContactEntity = ConvertUtil.convert(employeeContactDTO, EmployeeContactEntity.class);
                employeeContactEntity.setEmployeeId(employeeId);

                return employeeContactEntity;
            }).collect(Collectors.toList());
            this.saveBatch(employeeContactEntityList);
        }
    }
}
